package smedi.ismedi.web.bff.controller.constructionproject;

import com.smedi.ismedi.constructionproject.api.dto.request.BatchExistingPersonRequest;
import com.smedi.ismedi.constructionproject.api.dto.request.ProjectAssignmentRequest;
import com.smedi.ismedi.constructionproject.api.dto.response.ProjectAssignmentResponse;
import com.smedi.ismedi.constructionproject.api.feign.RemoteProjectAssignmentFeignApi;
import com.smedi.ismedi.web.starter.common.PaginationData;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.personnel.api.dto.response.PersonSimpleResponse;
import com.smedi.ismedi.personnel.api.feign.RemotePersonCoreFeignApi;
import com.smedi.ismedi.web.starter.exception.LogicException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import smedi.ismedi.web.bff.controller.BaseController;
import smedi.ismedi.web.bff.dto.PersonPickerResponse;
import smedi.ismedi.web.bff.infrastructure.constant.CustomError;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 项目分配接口
 *
 * @author xulinjun
 */
@Slf4j
@RestController
@Api(tags = {"项目分配接口"})
@RequestMapping("/webbff/v1/projectAssignment")
@AllArgsConstructor
public class ProjectAssignmentController extends BaseController {
    private final RemoteProjectAssignmentFeignApi remoteProjectAssignmentFeignApi;

    private final RemotePersonCoreFeignApi remotePersonCoreFeignApi;

    /**
     * 根据项目id获取人员分配的值列表
     *
     * @param
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "根据项目id获取人员分配的值列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目id", dataType = "Long", dataTypeClass = Long.class)
    })
    @GetMapping(value = "/getPersonPickerByProjectId")
    public ResponseData<List<PersonPickerResponse>> getPersonPickerByProjectId(@RequestParam("projectId") Long projectId) {
        ResponseData<List<Long>> responseData = remoteProjectAssignmentFeignApi.getQueryPersonIdsByProjectId(projectId);
        if (!responseData.isSuccess()) {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, responseData.getApplication(), String.valueOf(responseData.getErrorMsg()));
        }
        List<Long> personIds = responseData.getData();

        // 根据 personIds 获取人员信息
        ResponseData<List<PersonSimpleResponse>> personResponseData = remotePersonCoreFeignApi.getQueryPersonSimpleList(personIds);
        if (!personResponseData.isSuccess()) {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, personResponseData.getApplication(), String.valueOf(responseData.getErrorMsg()));
        }

        List<PersonPickerResponse> pickers = new ArrayList<>();

        List<PersonSimpleResponse> personSimples = personResponseData.getData();
        personSimples.stream().forEach(person -> {
            pickers.add(new PersonPickerResponse().setPersonId(person.getPersonId()).setLastName(person.getLastName()));
        });

        return new ResponseData<List<PersonPickerResponse>>().success(pickers);
    }

    /**
     * 保存项目分配信息
     *
     * @param
     * @return
     */
    @ApiOperation(httpMethod = "POST", value = "保存项目分配信息")
    @PostMapping(value = "/save")
    public ResponseData<ProjectAssignmentResponse> postSave(@Validated @RequestBody ProjectAssignmentRequest projectAssignmentRequest) {
        return remoteProjectAssignmentFeignApi.postSave(projectAssignmentRequest);
    }

    /**
     * 批量保存已存在人员
     *
     * @param batchExistingPersonRequest
     * @return
     */
    @ApiOperation(httpMethod = "POST", value = "批量保存已存在人员")
    @PostMapping(value = "/batchAddExistingPerson")
    public ResponseData<Boolean> postBatchAddExistingPerson(@Validated @RequestBody BatchExistingPersonRequest batchExistingPersonRequest) {
        return remoteProjectAssignmentFeignApi.postBatchAddExistingPerson(batchExistingPersonRequest);
    }

    /**
     * 删除人员项目分配
     *
     * @param projectAssignmentId
     * @return
     */
    @ApiOperation(httpMethod = "DELETE", value = "删除人员项目分配")
    @DeleteMapping(value = "/deleteProjectAssignment")
    public ResponseData<Boolean> deleteProjectAssignment(@RequestParam(value = "projectAssignmentId") Long projectAssignmentId) {
        return remoteProjectAssignmentFeignApi.deleteProjectAssignment(projectAssignmentId);
    }

    /**
     * 项目组织、项目管理页面查看项目组织下的人员信息表格
     *
     * @param offset
     * @param pageSize
     * @param projectId
     * @param projectOrganizationId
     * @param lastName
     * @param userName
     * @param mobile
     * @param containProjectOrganizationChildren
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "项目组织、项目管理页面查看项目组织下的人员信息表格")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "offset", value = "页码", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "每页行数", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目id", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(paramType = "query", name = "projectOrganizationId", value = "项目组织id", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(paramType = "query", name = "lastName", value = "姓名", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(paramType = "query", name = "userName", value = "登录名", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(paramType = "query", name = "employeeNum", value = "工号", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(paramType = "query", name = "mobile", value = "手机号", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(paramType = "query", name = "containProjectOrganizationChildren", value = "是否包含下级", dataType = "boolean", dataTypeClass = Boolean.class),
    })
    @GetMapping(value = "/queryProjectAssignmentPersonInfoPagination")
    public ResponseData<PaginationData<ProjectAssignmentResponse>> getQueryProjectAssignmentPersonInfoPagination(@RequestParam("offset") Long offset,
                                                                                          @RequestParam("pageSize") Long pageSize,
                                                                                          @RequestParam("projectId") Long projectId,
                                                                                          @RequestParam(value = "projectOrganizationId", required = false) Long projectOrganizationId,
                                                                                          @RequestParam(value = "lastName", required = false) String lastName,
                                                                                          @RequestParam(value = "userName", required = false) String userName,
                                                                                          @RequestParam(value = "employeeNum", required = false) String employeeNum,
                                                                                          @RequestParam(value = "mobile", required = false) String mobile,
                                                                                          @RequestParam(value = "containProjectOrganizationChildren", required = false) boolean containProjectOrganizationChildren) {
        return remoteProjectAssignmentFeignApi.getQueryProjectAssignmentPersonInfoPagination(offset, pageSize, projectId, projectOrganizationId, lastName, userName, employeeNum, mobile, containProjectOrganizationChildren);
    }

    /**
     * 用户管理界面根据项目id获取被分配的人员id
     *
     * @param projectId
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "用户管理界面根据项目id获取被分配的人员id")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目id", dataType = "Long", dataTypeClass = Long.class),
    })
    @GetMapping(value = "/queryPersonIdsByProjectId")
    public ResponseData<List<Long>> getQueryPersonIdsByProjectId(@RequestParam("projectId") Long projectId) {
        return remoteProjectAssignmentFeignApi.getQueryPersonIdsByProjectId(projectId);
    }

    /**
     * 用户管理界面根据人员id数组获取项目名称拼接
     *
     * @param personIds
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "用户管理界面根据人员id数组获取项目名称拼接")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "personIds", value = "人员id数组", dataType = "List", dataTypeClass = List.class),
    })
    @GetMapping(value = "/queryProjectNamesByPersonIds")
    public ResponseData<Map<Long, String>> getQueryProjectNamesByPersonIds(@RequestParam("personIds") List<Long> personIds) {
        return remoteProjectAssignmentFeignApi.getQueryProjectNamesByPersonIds(personIds);
    }

    /**
     * 根据人员id获取项目分配信息
     *
     * @param personId
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "根据人员id获取项目分配信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "personId", value = "人员id", dataType = "Long", dataTypeClass = Long.class),
    })
    @GetMapping(value = "/queryProjectAssignmentsByPersonId")
    public ResponseData<List<ProjectAssignmentResponse>> getQueryProjectAssignmentsByPersonId(@RequestParam("personId") Long personId) {
        return remoteProjectAssignmentFeignApi.getQueryProjectAssignmentsByPersonId(personId);
    }

    /**
     * 根据人员id和项目id删除项目分配信息
     *
     * @param personId
     * @return
     */
    @ApiOperation(httpMethod = "DELETE", value = "根据人员id和项目id删除项目分配信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "personId", value = "人员id", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目id", dataType = "Long", dataTypeClass = Long.class),
    })
    @DeleteMapping(value = "/deleteProjectAssignmentsByPersonIdAndProjectId")
    public ResponseData<Boolean> deleteProjectAssignmentsByPersonIdAndProjectId(@RequestParam("personId") Long personId,
    @RequestParam("projectId") Long projectId) {
        return remoteProjectAssignmentFeignApi.deleteProjectAssignmentsByPersonIdAndProjectId(personId,projectId);
    }

    /**
     * 根据人员id删除项目分配信息
     *
     * @param personId
     * @return
     */
    @ApiOperation(httpMethod = "DELETE", value = "根据人员id删除项目分配信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "personId", value = "人员id", dataType = "Long", dataTypeClass = Long.class),
    })
    @DeleteMapping(value = "/deleteProjectAssignmentsByPersonId")
    public ResponseData<Boolean> deleteProjectAssignmentsByPersonId(@RequestParam("personId") Long personId) {
        return remoteProjectAssignmentFeignApi.deleteProjectAssignmentsByPersonId(personId);
    }

    /**
     * 根据项目组织id判断是否存在分配信息
     *
     * @param projectOrganizationId
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "根据项目组织id判断是否存在分配信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "projectOrganizationId", value = "项目组织id", dataType = "Long", dataTypeClass = Long.class),
    })
    @GetMapping(value = "/validateProjectOrganizationHasAssignment")
    public ResponseData<Boolean> getValidateProjectOrganizationHasAssignment(@RequestParam("projectOrganizationId") Long projectOrganizationId) {
        return remoteProjectAssignmentFeignApi.getValidateProjectOrganizationHasAssignment(projectOrganizationId);
    }
}
